package com.atguigu.day04;

import com.atguigu.utils.ClickEvent;
import com.atguigu.utils.ClickSource;
import com.atguigu.utils.UrlCountPerWindow;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

// 统计的是每个url在每个5秒钟的滚动窗口中的访问次数
// 增量聚合函数和全窗口聚合函数结合使用进行优化
public class Example6 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env
                .addSource(new ClickSource())
                .keyBy(r -> r.url)
                // 5秒钟的滚动窗口
                .window(TumblingProcessingTimeWindows.of(Time.seconds(5)))
                .aggregate(new CountAgg(), new WindowResult())
                .print();

        env.execute();
    }

    // AggregateFunction<IN, ACC, OUT>
    public static class CountAgg implements AggregateFunction<ClickEvent, Long, Long> {
        // 创建空累加器
        @Override
        public Long createAccumulator() {
            return 0L;
        }

        // 定义输入数据和累加器的聚合规则
        @Override
        public Long add(ClickEvent value, Long accumulator) {
            return accumulator + 1L;
        }

        // 窗口闭合的时候，获取计算结果
        // 然后将计算结果发送给WindowResult中的process函数
        @Override
        public Long getResult(Long accumulator) {
            return accumulator;
        }

        @Override
        public Long merge(Long a, Long b) {
            return null;
        }
    }

    // 注意输入的泛型是Long！！！
    public static class WindowResult extends ProcessWindowFunction<Long, UrlCountPerWindow, String, TimeWindow> {
        @Override
        public void process(String key, Context context, Iterable<Long> elements, Collector<UrlCountPerWindow> out) throws Exception {
            // 迭代器中只有一个元素，就是getResult的返回值
            String url = key;
            Long count = elements.iterator().next(); // 取出迭代器中的唯一元素
            Long windowStart = context.window().getStart();
            Long windowEnd = context.window().getEnd();
            out.collect(new UrlCountPerWindow(url, count, windowStart, windowEnd));
        }
    }
}
